package com.example.myapplication.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.example.myapplication.R
import com.example.myapplication.R.styleable.*
import com.example.myapplication.databinding.ToolbarBinding

class CustomerToolbar : ConstraintLayout, View.OnClickListener {
    lateinit var binding: ToolbarBinding

    constructor(context: Context) : super(context) {
        init(null, 0, 0)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs, 0, 0)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init(attrs, defStyleAttr, 0)
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        init(attrs, defStyleAttr, defStyleRes)
    }


    private fun init(attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) {
        val typedArray = context.obtainStyledAttributes(
            attrs,
            CustomerToolbar,
            defStyleAttr,
            defStyleRes
        )
        val scaledDensity = context.resources.displayMetrics.scaledDensity
        val title = typedArray.getString(CustomerToolbar_text)
        val textSize = typedArray.getDimension(CustomerToolbar_textSize, 0f)
        val textColor = typedArray.getColor(CustomerToolbar_textColor, Color.BLACK)
        binding = ToolbarBinding.inflate(LayoutInflater.from(context), this, true)

        binding.tvTitle.setTextColor(textColor)

        val background = typedArray.getDrawable(CustomerToolbar_background)
        val rightBg = typedArray.getDrawable(CustomerToolbar_rightBg)
        val leftBg = typedArray.getDrawable(CustomerToolbar_leftBg)

        if (background != null) {
            binding.root.background = background
        }

        if (leftBg != null) {
            binding.ivLeft.background = leftBg
        }
        if (rightBg != null) {
            binding.ivRight.background = rightBg
        }
        // 判断是否设置了字体大小，如果未设置，改变默认字体大小为16sp
        if (textSize > 0) {
            binding.tvTitle.paint.textSize = textSize
        } else {
            binding.tvTitle.paint.textSize = (scaledDensity * 16 + 0.5f)
        }


        if (!title.isNullOrEmpty()) {
            binding.tvTitle.text = title
        }

        binding.ivLeft.setOnClickListener(this)
        binding.ivRight.setOnClickListener(this)
        typedArray.recycle()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ivLeft -> {
                clickListener?.onBackListener()
            }
            R.id.ivRight -> {
                clickListener?.onRightListener()
            }
        }
    }

    fun setTitle(title: String?) {
        binding.tvTitle.text = title
    }

    fun getTitle(): String {
        return binding.tvTitle.text.toString()
    }

    interface ClickListener {
        fun onBackListener()

        fun onRightListener()
    }

    var clickListener: ClickListener? = null
}